package com.cragon.boot.loader.data;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @Description: TODO
 * @Author: ZHOUHAILONG
 * @Date: 2020年09月17日 11:47
 */
public class ByteArrayRandomAccessData implements RandomAccessData {
    private final byte[] bytes;
    private final long offset;
    private final long length;

    public ByteArrayRandomAccessData(byte[] bytes) {
        this(bytes, 0L, bytes != null ? (long)bytes.length : 0L);
    }

    public ByteArrayRandomAccessData(byte[] bytes, long offset, long length) {
        this.bytes = bytes != null ? bytes : new byte[0];
        this.offset = offset;
        this.length = length;
    }

    public InputStream getInputStream(ResourceAccess access) {
        return new ByteArrayInputStream(this.bytes, (int)this.offset, (int)this.length);
    }

    public RandomAccessData getSubsection(long offset, long length) {
        return new ByteArrayRandomAccessData(this.bytes, this.offset + offset, length);
    }

    public long getSize() {
        return this.length;
    }
}

